{


    var o = ["room_online_info", "heart_beat"];
    Global.TCP = {
        CONNECT_STATUS_OK: 1,
        CONNECT_STATUS_OPENING: 2,
        CONNECT_STATUS_CLOSING: 3,
        CONNECT_STATUS_FAIL: 4,
        connectStatus: 0,
        tickCount: 0,
        isTimerInited: !1,
        connect: function (e) {
            var t = this;
            if (console.info("TCP status  = ", Global.TCP.connectStatus), Global.TCP.connectStatus == Global.TCP.CONNECT_STATUS_OK) return Global.notificationCenter.emit(Global.eventName.GAME_NETWORK_CONNECT_SUCCESSED),
                void console.info("TCP is OK ！无需重连");
            Global.TCP.connectStatus != Global.TCP.CONNECT_STATUS_OPENING ? (Global.TCP.connectStatus = Global.TCP.CONNECT_STATUS_OPENING,
                wx.onSocketOpen(function (e) {
                    console.info("TCP webSocket opened..."), Global.TCP.connectStatus = Global.TCP.CONNECT_STATUS_OK,
                        Global.notificationCenter.emit(Global.eventName.GAME_NETWORK_CONNECT_SUCCESSED),
                    Global.TCP.isTimerInited || (Global.TCP.isTimerInited = !0), tywx.Timer.setTimer(cc.director, t.timerSchedule.bind(t), 1);
                }), wx.onSocketError(function (e) {
                console.info("TCP webSocket error... res" + JSON.stringify(e)), Global.TCP.connectStatus = Global.TCP.CONNECT_STATUS_FAIL,
                    Global.notificationCenter.emit(Global.eventName.GAME_NETWORK_CONNECT_FAILED);
            }), wx.onSocketClose(function (e) {
                console.info("WebSocket 已关闭！"), Global.TCP.connectStatus = Global.TCP.CONNECT_STATUS_CLOSING,
                    Global.notificationCenter.emit(Global.eventName.GAME_NETWORK_CONNECT_CLOSE);
            }), wx.onSocketMessage(function (e) {
                Global.notificationCenter.emit(Global.eventName.NET_RECIVE_MSG, e);
            }), wx.connectSocket({
                url: e
            })) : console.info("TCP is OPENING ！无需重连");
        },
        timerSchedule: function () {
            if (!Global._isInBackground && (Global.TCP.connectStatus == Global.TCP.CONNECT_STATUS_FAIL || Global.TCP.connectStatus == Global.TCP.CONNECT_STATUS_CLOSING)) {
                if (Global.netWork.isLogout()) return;
                console.info("------通过心跳包重连网络-------"), this.connect(Global.config.host);
            }
        },
        sendMsg: function (e) {
            if (console.info("WX TCP sendMsg0: " + Global.TCP.connectStatus), Global.TCP.connectStatus == Global.TCP.CONNECT_STATUS_CLOSING) return console.info("---------wx CONNECT_STATUS_CLOSING 状态 send 不了-------------,", Global.TCP.connectStatus),
                void Global.notificationCenter.emit(Global.eventName.GAME_NETWORK_CONNECT_CLOSE);
            if (Global.TCP.connectStatus == Global.TCP.CONNECT_STATUS_OK) {
                var t = JSON.stringify(e);
                -1 == o.indexOf(e.cmd) && console.info("WX TCP sendMsg: " + t), wx.sendSocketMessage({
                    data: t,
                    success: function (e) {
                    },
                    fail: function (e) {
                        console.info("sendSocketMessage fail:", e);
                        var t = arguments[0];
                        t && "sendSocketMessage:fail taskID not exist" === t.errMsg && (wx.closeSocket(),
                            Global.TCP.connectStatus = Global.TCP.CONNECT_STATUS_CLOSING);
                    },
                    complete: function (e) {
                    }
                });
            } else console.info("---------wx 网络断开了 send 不了-------------,", Global.TCP.connectStatus);
        },
        close: function (e) {
            console.info("TCP close :", Global.TCP.connectStatus), Global.TCP.connectStatus == Global.TCP.CONNECT_STATUS_OK ? wx.closeSocket({
                reason: "socket connect error",
                success: function () {
                    console.info("TCP close success。。。"), e && e();
                },
                fail: function () {
                    console.info("TCP close fail.."), e && e();
                }
            }) : (console.error("TCP close error:", Global.TCP.connectStatus), e && e());
        }
    }
}